package com.example.administrator.hello;

import android.content.Context;
import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.Until;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.provider.ContactsContract.Directory.PACKAGE_NAME;
import static org.junit.Assert.*;

/**
 * Instrumentation test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class helloTest {
    private UiDevice device;


    @Test
    public void name() throws Exception {
        UiObject2 firstNumber=device.findObject(By.res("com.example.administrator.hello:id/first"));
        firstNumber.setText("10");
        UiObject2 secondNumber=device.findObject(By.res("com.example.administrator.hello:id/second"));
        secondNumber.setText("20");
        Thread.sleep(3000);
        UiObject2 buttontext=device.findObject(By.res("com.example.administrator.hello:id/calc"));
        buttontext.click();
        UiObject2 resulttext=device.findObject(By.res("com.example.administrator.hello:id/result"));
        String actualValue=resulttext.getText().toString();
        String expectValue="30";
        assertEquals(expectValue,actualValue);
    }

    @Before
    public void setUp() throws Exception {
        // 实例化UiDevice
        device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        // 启动应用
        //获取上下文
        Context context = InstrumentationRegistry.getContext();
        //通过将包名传给包管理器获取启动的intent
        final Intent intent = context.getPackageManager()
                .getLaunchIntentForPackage("com.example.administrator.hello");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        //启动应用
        context.startActivity(intent);
        // 等待应用启动
        device.wait(Until.hasObject(By.pkg("com.example.administrator.hello").depth(0)),
                10000);



    }

    @After
    public void tearDown() throws Exception {


    }
}
